/******************* (C) COPYRIGHT 2022 CHU WEI TENG ***********************************************
**
**------------------ File Info ---------------------------------------------------------------------
** File name:      example_info_page.c
** modified Date:  2022-05-10
** Last Version:   V1.0
** Descriptions: 
** 
**------------------ Modfication  log --------------------------------------------------------------
** Date       Who            Rev        Comments
----------------------------------------------------------------------------------------------------
   22/05/10 chuwt@qq.com    V1.00      created

****************************************************************************************************/
#include "example_info_page.h"

/* Private define----------------------------------------------------------------------------------*/

/* Private typedef---------------------------------------------------------------------------------*/
/* Private variables-------------------------------------------------------------------------------*/
dwin_widget_vtable_t  example_info_page_vt = 
{
    .obj_size = sizeof(example_info_page_t),
};

/* public function---------------------------------------------------------------------------------*/
dwin_widget_t* example_info_page_create(dwin_widget_t* parent, uint16_t addr, uint16_t attr, uint16_t size)
{
    dwin_widget_t* widget = dwin_widget_create(parent, &example_info_page_vt, 0);

    if (widget != NULL)
    {
        uint16_t        offset = 0;
                
        example_info_page_t*  page = (example_info_page_t *)widget;

        dwin_page_init(widget, EXAMPLE_INFO_PAGE_ID, addr, attr, size);
        page->parent.read = example_info_data_read;
        page->parent.write = example_info_data_write;

		/*create label_gbk*/
		page->number = dwin_label_gbk_create(&page->parent.widget, EXAMPLE_INFO_ADDR + offset, EXAMPLE_INFO_ATTR, 6);
        dwin_page_widget_add(widget, page->number);
        offset += 3;
		
		/*create prev button*/
        page->prev = dwin_button_create(&page->parent.widget, EXAMPLE_INFO_ADDR + offset, EXAMPLE_INFO_ATTR, 2);
        dwin_page_widget_add(widget, page->prev);
		dwin_widget_event_register(page->prev, EVT_CLICK, event_prev_func);
        offset++;

		/*create next button*/
        page->next = dwin_button_create(&page->parent.widget, EXAMPLE_INFO_ADDR + offset, EXAMPLE_INFO_ATTR, 2);
        dwin_page_widget_add(widget, page->next);
		dwin_widget_event_register(page->next, EVT_CLICK, event_next_func);
    }
    return widget;
}

static ret_t example_info_data_read(dwin_widget_t* widget)
{
    return_value_if_fail(widget, RET_BAD_PARAMS);

    int  len = 0;
    char str[10] = {0};
    example_info_page_t*  page = (example_info_page_t *)widget;
    
    len = sprintf(str, "%01d/%01d", page->page_number + 1, page->page_number_max);
    memcpy(page->number->data, str, len);
	
	return RET_OK;
}

static ret_t example_info_data_write(dwin_widget_t* widget)
{ 
    return_value_if_fail(widget, RET_BAD_PARAMS);
    
    example_info_page_t*  page = (example_info_page_t *)widget;
    
    dwin_label_gbk_write(page->number);
    
    return RET_OK;
}

static ret_t event_prev_func(dwin_widget_t* widget)
{
    return_value_if_fail(widget, RET_BAD_PARAMS);
    
    example_info_page_t*   page = (example_info_page_t*)widget->parent;
    
    if (page->page_number > 0)
    {
        page->page_number--;
    }
    return RET_OK;
}

static ret_t event_next_func(dwin_widget_t* widget)
{
	return_value_if_fail(widget, RET_BAD_PARAMS);
	
    example_info_page_t*   page = (example_info_page_t*)widget->parent;
    
    if (page->page_number < (page->page_number_max - 1))
    {
        page->page_number++;
    }

    return RET_OK;
}
